package commonClasses.Messages;

import commonClasses.*;
import commonClasses.commonInterfaces.MsgIntf;
import commonClasses.commonInterfaces.ProtocolIntf;

/**
 *
 * @author Pozytywny!
 */
public class FileMsg extends Msg implements MsgIntf{


    private byte _arr[];
    private long _length;

    public FileMsg(ProtocolIntf protocolIntf, byte[] arr) {
        super(protocolIntf, MagicNumbers.FILE_MSG_NR);
        this._arr = arr;
    }

    public FileMsg(ProtocolIntf protocolIntf) {
        super(protocolIntf, MagicNumbers.FILE_MSG_NR);
        this._arr = null;
    }

    public void receive() throws Exception {
        
        long length = receiveUint();
        _length = length;
        Utils.received("FM:"+_length+" :"+Msg.longToInt(_length));
        _arr = receiveByteArr(Msg.longToInt(_length));
        Utils.received("FM:wczytałem: " + _arr.length);
        if(_arr==null) throw new Exception("nie udało się wcztać pliku");
    }

    public void send() throws Exception {
        if(_arr == null) throw new Exception("Byte array not set," +
                " wrong constructor used");
        
        Utils.write("wysyłam FILE");
        int msgID[] = idToIntArr();
        sendIntArr(msgID);
        Utils.sent(intArrToLong(idToIntArr()));
        int length[] = longToArr(_arr.length);
        sendIntArr(length);
        Utils.sent(_arr.length);

        sendByteArr(_arr);
    }
    
    public byte[] getArr() {
        return _arr;
    }

    public long getLength() {
        return _length;
    }



}
